#! /bin/bash

curPath=$(dirname $(readlink -f "$0"))
REDIS_HOME=${curPath}/redis
REDIS_CONFIG=${REDIS_HOME}/redis.conf
REDIS_BIN=${REDIS_HOME}/bin
REDIS_CLOSE_PORT=61001
REDIS_CLOSE_PASSWORD=12345678 

start()
{
    tmp=`ps -ef | grep "./redis-server 0.0.0.0:${REDIS_CLOSE_PORT}" | wc -l`
    if [ $tmp -gt 1 ]; then 
      echo "The server arealdy started...abort!" 
      exit 1 
    fi 
    cd ${REDIS_BIN}
    ./redis-server ../redis.conf
    sleep 2
    ps -ef | grep "${REDIS_BIN}/redis-server 0.0.0.0:${REDIS_CLOSE_PORT}"
    echo "Start redis server in ${REDIS_HOME} OK!"    
}


stop()
{ 
   cd ${REDIS_BIN}
    ./redis-cli -c -p ${REDIS_CLOSE_PORT} -a ${REDIS_CLOSE_PASSWORD}  shutdown
    echo "Stopped REDIS server"
}


usage()
{
        echo "Usage: $0 [start|stop|restart]"
}


if [ $# -lt 1 ];then
        usage
        exit
fi

if [ "$1" = "start" ];then
        start

elif [ "$1" = "stop" ];then
        stop

elif [ "$1" = "restart" ];then
        stop
        start

else
        usage
fi

